// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
    {                                         \
        cerr << "\033[32;1m" << #x << " -> "; \
        err(x);                               \
    }
void err() { cerr << "\033[39;0m" << endl; }
template <typename T, typename... A>
void err(T a, A... x) {
    cerr << a << ' ';
    err(x...);
}
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 1e6 + 50;

int t, n;
char s[N];

int main(int argc, char const *argv[]) {
    // code
    scanf("%d", &n);
    scanf("%s", s);
    int n1 = 0, n2 = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '(')
            ++n1;
        else
            ++n2;
    }
    if (n1 != n2) {
        puts("-1");
        return 0;
    }

    n1 = n2 = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '(')
            ++n1;
        else if (s[i] == ')') {
            if (n1 > 0)
                --n1;
            else {
                ++n2;
                int j = i;
                while (j + 1 < n && n1 != n2) {
                    ++j;
                    if (s[j] == '(')
                        ++n1;
                    else
                        ++n2;
                }
                ans += j - i + 1;

                // dbg(i, j);
                i = j;
                n1 = n2 = 0;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
